import React from 'react';
import ReactDOM from 'react-dom';
import { Toolbar } from "./toolbar/copyIframeSrc";
import { YanFaYiTiHuaInit } from "./toolbar/yyrd";
import { jiraInit } from "./toolbar/Jira";
import { LoginQuikly } from "./LoginBtns";
import { NccInit } from "./toolbar/copyIframeSrc";

import { onReady,baseData } from './init.js'
import { run,autoLogin,autoLogout } from './methods'
import './index.css'
/* 
  注意
    1、脚本是在整个网页window下执行的，所以引入的自定义css不会对里头的iframe中的元素样式生效

*/
try {
  window.addEventListener('load',() =>{
    let fn = ()=>{
      let App = null
      let documentAPP = baseData.mainIframeTopDocument
      // 项目登录页面
      if (baseData.loginPage) {
        App = <LoginQuikly />  
        // if (!MySwitchApi.checked) return
        autoLogin()
      } else {
        // 判断是NCC项目网址
        if (window.top.location.href.includes('resources/workbench')) {
          App = <Toolbar /> 
        }
        // autoLogout()
      }
      let div = documentAPP.createElement('div')
      div.id = 'nccExtension'
      Object.assign(div.style,{position:'fixed',zIndex:'99999', right: 0,bottom:'40%',zIndex:999999,display: 'flex', flexDirection: 'column'})
      documentAPP.body.append(div)
      ReactDOM.render(
          App,
        documentAPP.getElementById('nccExtension')
      );        
    }
    onReady().then(() => {
      fn()   
    })
    try {   
      if (baseData.loginPage) {
        // 切换账套需要重选渲染
        setInterval(() => {
          let forwardFrame = document.querySelector('#forwardFrame')
          if (forwardFrame) {
            let nccExtension = forwardFrame.contentDocument.querySelector('#nccExtension')
            if (!nccExtension) {
              onReady().then(() => {
                fn()   
              })
            }
          }
        }, 500);
      }   
    } catch (error) {
      
    }


    // yyrd
    if (window.top.location.href.includes('pm.yyrd.com')) {
      YanFaYiTiHuaInit()
    }
    // jira网站
    if (window.top.location.href.includes('nccjira')) {
      jiraInit()
    }
    if (window.top.location.href.includes('resources/workbench')) {
      NccInit()
    } 
    //   
  })
} catch (error) {
  

}

